<template>
  <div class="userr">
    <div class="qqq">
      <div>
        <h2>个人信息</h2>
      </div>
      <div class="head">
        <img class="iii" :src="form.img" />
      </div>
      <div>
        <h2>id:{{ form.adminId }}</h2>
      </div>
      <div>
        <h2>帐号:{{ form.adminName }}</h2>
      </div>
      <div>
        <h2>手机号码:{{ form.phoneNumber }}</h2>
      </div>
      <div>
        <h2>性别:{{ form.sex == "1" ? "男" : "女" }}</h2>
      </div>
      <div>
        <h2>职位:{{ form.position }}</h2>
      </div>
      <div>
        <h2>权限:{{ form.state == "1" ? "权限正常" : "权限异常" }}</h2>
      </div>
      <div class="btnRow">
        <el-button type="primary" @click="edit">修改个人信息</el-button>
      </div>
    </div>
    <el-dialog title="个人信息修改" :visible.sync="dialogVisible" width="30%" :before-close="handleClose">
      <div class="bbb">
        <el-upload
          class="avatar-uploader"
          action="/api/CjwAdmin/UploadPictures"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
        >
          <img v-if="imageUrl" :src="imageUrl" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </div>
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="用户名">
          <el-input v-model="form.adminName" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="电话号码">
          <el-input v-model="form.phoneNumber"></el-input>
        </el-form-item>
        <el-form-item label="身份证">
          <el-input v-model="form.idNumber"></el-input>
        </el-form-item>
        <el-form-item label="生日">
          <el-date-picker v-model="form.birth" type="date" placeholder="选择日期"></el-date-picker>
        </el-form-item>
        <el-form-item label="性别">
          <el-radio v-model="form.sex" label="0">女</el-radio>
          <el-radio v-model="form.sex" label="1">男</el-radio>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="formComfirm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "",
  props: {
    adminName: {
      type: String,
      default: ""
    }
  },
  data() {
    return {
      adminId: 1,
      imgUrl: "",
      form: {
        adminName: "",
        phoneNumber: "",
        idNumber: ""
      },
      dialogVisible: false,
      imageUrl: "",
      url: ""
    };
  },
  created() {
    this.getMyInformation();
  },
  methods: {
    edit() {
      this.dialogVisible = true;
    },
    getMyInformation() {
      this.$axios
        .get(
          "/api/CjwAdmin/GetMyInformationByAdminName?adminName=" +
            this.adminName
        )
        .then(res => {
          this.form = res.data;
          this.imageUrl = res.data.img;
          console.log(this.form);
        });
    },
    handleClose() {
      this.dialogVisible = false;
    },
    handleAvatarSuccess(res, file) {
      // console.log(res);
      // console.log(file);
      this.imageUrl = res;
      this.url = res;
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
    formComfirm() {
      console.log(this.form);
      let params = {
        adminId: this.form.adminId,
        adminName: this.form.adminName,
        adminPassword: this.form.adminPassword,
        phoneNumber: this.form.phoneNumber,
        idNumber: this.form.idNumber,
        birth: this.form.birth,
        sex: this.form.sex,
        position: this.form.position,
        state: 1,
        img: this.imageUrl
      };
      console.log(params);
      this.$axios.post("/api/CjwAdmin/UpdateAdmin", params).then(res => {
        console.log(res);
        this.getMyInformation();
        this.$message({
          message: "修改成功",
          type: "success"
        });
        this.dialogVisible = false;
        let op = {
          opAdminName: this.adminName,
          opTable: "修改员工信息",
          opData: `员工id:${params.adminId}`
        };
        this.$axios.post("/api/CjwOperationList/InsertOpData", op);
      });
    }
  }
};
</script>

<style scoped>
.qqq {
  display: flex;
  flex-direction: column;
  justify-content: center;
  text-align: center;
}
.btnRow {
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.bbb {
  display: flex;
  justify-content: center;
}

.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.iii {
  width: 200px;
  height: 200px;
}
</style>